package com.sysmanage.permissions.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sysmanage.common.tools.page.Pagination;
import com.sysmanage.framework.dao.hspring.DAOInterface;
import com.sysmanage.framework.service.BaseService;
import com.sysmanage.permissions.dao.LogDao;
import com.sysmanage.permissions.model.Logs;
import com.sysmanage.permissions.service.ILogService;

@Service
@Transactional
public class LogService extends BaseService<Logs> implements ILogService{
	public static final int LOG_MAX_SIZE = 200;
	@Autowired
	private LogDao logDao;
	
	public Pagination getPageList(Logs log,int page,int rows,String sort,String order){
		return logDao.getPageList(log, page, rows,sort,order);
	}
	
	@Override
	protected DAOInterface<Logs> getDAO() {
		return logDao;
	}
	public void log(String content,String opertype)
	{

		try {
			//当前用户
			String user = (String) SecurityUtils.getSubject().getPrincipal();
			//操作类型
			//ip
			InetAddress host = InetAddress.getLocalHost();
			String ip = host.getHostAddress();

			//日志时间
			Date date = new Date();
			java.text.SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			if(content!=null&&content.length()>LOG_MAX_SIZE)
			{
				content = content.substring(0,LOG_MAX_SIZE-8)+"....";
			}
			
			Logs log = new Logs(1, opertype, content, 0L, ip, user,format.format(date), null);
			this.save(log);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
